package com.aos.erp.message.service.impl;


import com.aos.erp.common.bean.IPageBean;
import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.dao.mapper.Condition;
import com.aos.erp.common.exception.BizException;
import com.aos.erp.message.enums.DelStatus;
import com.aos.erp.message.mapper.MTParamMapper;
import com.aos.erp.message.model.MTParamInfo;
import com.aos.erp.message.model.vo.MTParamVo;
import com.aos.erp.message.service.MTParamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class MTParamServiceImpl extends ServiceImpl<MTParamMapper,MTParamInfo> implements MTParamService {

    @Override
    public MTParamInfo getById(long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public void saveParamItem(MTParamVo dataInfo) throws BizException {

        checkSaveParamData(dataInfo);

        // 保存参数项
        saveParamItemData(dataInfo);
    }

    @Override
    public void updateParamItem(MTParamVo dataInfo) throws BizException {

        checkUpdateParamData(dataInfo);

        // 保存参数项
        baseMapper.updateParamItem(dataInfo);
    }

    @Override
    public void deleteParamItem(long dataId) throws BizException {

        if(dataId == 0) {
            throw new BizException("配置项ID不能为空");
        }
        baseMapper.deleteParamItem(dataId);
    }

    @Override
    public PageBean<MTParamInfo> queryByPage(long pageIndex, long pageSize) {

        IPageBean<MTParamInfo> pageBean = baseMapper.createBizPage(pageIndex, pageSize);
        Condition condition = new Condition(MTParamInfo.class);
        condition.eqFiled("isDel", DelStatus.del_no);
        condition.orderByDesc("id");

        return (PageBean<MTParamInfo>) baseMapper.selectPage(pageBean, condition.getWrapper());
    }

    @Override
    public List<MTParamInfo> queryAll() {
        Condition condition = new Condition(MTParamInfo.class);
        condition.eqFiled("isDel", DelStatus.del_no);
        condition.orderByDesc("id");

        return baseMapper.selectList(condition.getWrapper());
    }
    /**
     * 保存参数项
     * @param dataInfo
     * @return
     */
    private long saveParamItemData(MTParamVo dataInfo) {

        Date nowDate = new Date();
        MTParamInfo paramInfo = new MTParamInfo();
        BeanUtils.copyProperties(dataInfo, paramInfo);
        paramInfo.setIsDel(DelStatus.del_no);
        paramInfo.setCreateUser(dataInfo.getOptUser());
        paramInfo.setCreateTime(nowDate);
        paramInfo.setUpdateUser(dataInfo.getOptUser());
        paramInfo.setUpdateTime(nowDate);
        this.save(paramInfo);
        return paramInfo.getId();
    }

    private MTParamInfo getParamItemByKey(String mtKey) {
        Condition condition = new Condition(MTParamInfo.class);
        condition.eqFiled("mtpItem", mtKey);
        condition.eqFiled("isDel", DelStatus.del_no);
        return baseMapper.selectOne(condition.getWrapper());
    }

    private void checkParamData(MTParamVo params) throws BizException {
        if(params.getMtpItem().isEmpty()) {
            throw new BizException("参数项不能为空");
        }
        if(params.getMtpDesc().isEmpty()) {
            throw new BizException("参数项描述不能为空");
        }

    }

    private void checkSaveParamData(MTParamVo params) throws BizException {
        checkParamData(params);
        MTParamInfo mcData = getParamItemByKey(params.getMtpItem());
        if(mcData != null && mcData.getId() > 0) {
            throw new BizException("参数项已使用");
        }
    }

    private void checkUpdateParamData(MTParamVo params) throws BizException {
        if(params.getId() == 0) {
            throw new BizException("配置项不能为空");
        }
        checkParamData(params);
        MTParamInfo mcData = getParamItemByKey(params.getMtpItem());
        if(mcData != null && mcData.getId() != params.getId()) {
            throw new BizException("该配置项已被使用");
        }
    }
}